
package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import main.Terminal;
import model.Cinema;
import model.Movie;
import model.ShowTime;
/**
 * 
 * @author Allan Malka
 *
 */
public class DBShowTime
{
	//If any changes happens to the database change here.
	private final String id = "ID";
	private final String year = "Year";
	private final String month = "Month";
	private final String day = "Day";
	private final String hour = "Hour";
	private final String minute = "Minute";
	private final String cinNo = "CinemaNo";
	private final String movID = "MovieID";
	
	private DBMovie dbMov;
	private DBCinema dbCin;
	
	//String builder
	private final String selectAll = String.format("SELECT %s, %s, %S, %s, %s, %s, %s, %s FROM ShowTime ",
													id, year, month, day, hour, minute, cinNo, movID);
	
	private DBConnection conn;
	
	public DBShowTime()
	{
		conn = DBConnection.getInstance();
		dbCin = new DBCinema();
		dbMov = new DBMovie();
	}
	
	/**
	 * @deprecated  use getShowTimes(Cinema c) in stead
	 * @param c		The cinema object to find <i>a</i> seat from (whatever you want to use that for??)
	 * @return		A random ShowTime object somehow connected to the parsed cinema-object <b>c</b>
	 */
	public ShowTime getShowTime(Cinema c)
	{
		ShowTime result = null;
		
		ResultSet resSet = conn.executeResSet(whereAdder(selectAll, cinNo + " = " + c.getNumber()));
		try {
			if(resSet.next()) {	
				result = buildShowTime(resSet, c);
				try {
					resSet.getStatement().close();
				}
				catch(SQLException sE) {
					Terminal.say("Stack trace: ", sE);
				}
			}
		} catch (SQLException e) {
			Terminal.say("Could not get next result off of resSet in DBShowTime.getShowTIme", e);
		}
		
		return result;
		
	}
	
	public int getShowTimeID(ShowTime st)
	{
		int id = -1;
		String query = "SELECT " + this.id + " FROM ShowTime WHERE "
									+ this.year + " = '" + st.getYear() + "' AND "
									+ this.month + " = '" + st.getMonth() + "' AND "
									+ this.day + " = '" + st.getDay() + "' AND "
									+ this.hour + " = '" + st.getHour() + "' AND "
									+ this.minute + " = '" + st.getMinute() + "'";
		ResultSet resSet = conn.executeResSet(query);
		try {
			if(resSet.next()) {
				id = resSet.getInt(this.id);
				return id;
			}
		}
		catch(SQLException se) {
			Terminal.say("Stack trace: ", se);
		}
		
		return id;
	}
	
	public ArrayList<ShowTime> getShowTimes(Cinema c)
	{
		ArrayList<ShowTime> allShowTimes = new ArrayList<ShowTime>();
		
		ResultSet resSet = conn.executeResSet(whereAdder(selectAll, cinNo  + " = " + c.getNumber()));
		
		try {
		while(resSet.next()) {
			allShowTimes.add(buildShowTime(resSet, c));
		}
			resSet.getStatement().close();
		}
		catch(SQLException sE) {
			Terminal.say("Stack trace: ", sE);
		}
		
		return allShowTimes;
	}
	
	public ArrayList<ShowTime> getAllShowTimes()
	{
		ArrayList<ShowTime> allShowTimes = new ArrayList<ShowTime>();
	
			ResultSet resSet = conn.executeResSet(selectAll);
			
			try {
				while(resSet.next()) {
					ShowTime st = buildShowTime(resSet, dbCin.getCinema(resSet.getString("CinemaNo")));
					
					if(st != null)
					{
						allShowTimes.add(st);
					}
				}
					resSet.getStatement().close();
			}
			catch(SQLException sE) {
					Terminal.say("Stack trace: ", sE);
			}
			
		return allShowTimes;
	}
	
	private String whereAdder(String queryString, String condition)
	{
		return queryString + " WHERE " + condition;
	}
	
	private ShowTime buildShowTime(ResultSet resSet, Cinema c)
	{
		try {
			
			int year = resSet.getInt(this.year);
			int month = resSet.getInt(this.month);
			int day = resSet.getInt(this.day);
			int hour = resSet.getInt(this.hour);
			int minute = resSet.getInt(this.minute);
			int movID = resSet.getInt(this.movID);
			Movie m = dbMov.getMovie(movID);
			
			return new ShowTime(year, month, day, hour, minute, c, m);
		}
		catch(SQLException sE)  {
			Terminal.say("Could not build ShowTime", sE);
			
			return null;
		}
	}
}